package br.ufrj.neg.contatos.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.velocity.tools.view.VelocityViewServlet;

import br.ufrj.neg.contatos.bean.ContatoBean;
import br.ufrj.neg.contatos.model.ContatoModel;



public class RetornaContatosJSON extends VelocityViewServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4262895502518531473L;

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		response.setContentType("text/json; charset:utf-8");
		response.setCharacterEncoding("utf-8");
		
		String action = request.getParameter("action");
		
		if(action.equals("loadAll")){
			
			loadAll(request,response);
		}
		else if(action.equals("load")){
			
			load(request,response);
			
		}
		
		
	}
	
	private void load(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		PrintWriter out = response.getWriter(); 
		
		Integer idContato = Integer.parseInt(request.getParameter("idContato"));
		
		ContatoModel cm = new ContatoModel();
		ContatoBean cb = cm.getContato(idContato);
				
		JSONObject contact = new JSONObject(); 
	 	contact.element("idContato", cb.getIdContato());
	 	contact.element("nome", cb.getNome());
	 	contact.element("tel", cb.getTelefoneListagem());
	 	contact.element("cargo", cb.getCargo());
	 	contact.element("email", cb.getEmail());
	 	contact.element("endereco", cb.getEndereco());

		out.println(contact);
		
	}

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		doPost(request, response);
	}
	
	private void loadAll(HttpServletRequest request, HttpServletResponse response) throws IOException{
		
		PrintWriter out = response.getWriter(); 
		
		Integer codigoNegociacao = Integer.parseInt(request.getParameter("neg"));
		
		ContatoModel cm = new ContatoModel();
		ArrayList<ContatoBean> contatos = cm.getContatosPorNegociacao(codigoNegociacao);
				
		JSONObject saida = new JSONObject();
		
		saida.element("identifier", "id");
		saida.element("label","name");
		
		JSONArray items = new JSONArray();
		
		for (ContatoBean contatoBean : contatos) {
			
			JSONObject contato = new JSONObject(); 		
			
			contato.element("id",contatoBean.getIdContato());
			contato.element("name",contatoBean.getNome());
			
			contato.element("idContato", contatoBean.getIdContato());
			contato.element("nome", contatoBean.getNome());
			contato.element("tel", contatoBean.getTelefoneListagem());
			contato.element("cargo", contatoBean.getCargo());
			contato.element("email", contatoBean.getEmail());
			contato.element("endereco", contatoBean.getEndereco());
		 	
			items.add(contato);
		}
		
		
		
		saida.element("items", items);
		
		
		out.println(saida);
		
		
		
	}
	
}
